import sys, traceback, Ice

Ice.loadSlice("./chatroom.ice")

import Chat

class ChatRoomI(Chat.Room):
    def __init__(self):
        self.userList = {}
    def login(self, user, receiver, current=None):
        if user in self.userList  :
            return 0
        self.userList[user] = receiver
        return 1
    def logout(self, user, context=None):
        if user in self.userList :
            del self.userList[user]
            return 1
        return 0
    def broadcast(self, user, message, context=None):
        for key ,value in self.userList.items():
            if key != user:
                value.onMessage(user,message)
    def printUserList(self, current=None):
        print("userList = ", self.userList)


status =   0
ic  =   None
try:
    ic  =   Ice.initialize(sys.argv)
    adapter  =   ic.createObjectAdapterWithEndpoints( "ChatRoom" ,  "default -p 12345" )
    object   =   ChatRoomI()
    adapter.add( object , ic.stringToIdentity( "ChatRoom" ))
    adapter.activate()
    ic.waitForShutdown()
except :
    traceback.print_exc()
    status  =   1
  
if  ic:
    try :
        ic.destroy()
    except :
        traceback.print_exc()
        status  =   1